<?php
declare (strict_types=1);

namespace app\api\controller;

use app\api\model\ActType;
use app\api\model\CouponDsecModel;
use app\api\model\CouponModel;
use app\api\model\CouponType;
use app\api\model\Orders;
use think\Request;
use think\Validate;

class CouponController
{
    /**
     * 优惠券叠加使用
     * @return \think\Response
     */
    public static function useCoupon(Request $request)
    {
        $coupon_id = $request->param('coupon_id');
        if (!empty($coupon_id)) {
            $coupon = CouponDsecModel::selCoupons($coupon_id);
            $sum = 0;
            foreach ($coupon as $v) {
                $sum = $sum + $v['coupon_price'];
            }
            if (empty($coupon)) {
                return json(['code' => 4001, 'msg' => '出错咯', 'data' => false]);
            } else {
                return json(['code' => 2001, 'msg' => '成功', 'data' => $coupon, 'discount_amount' => $sum]);
            }
        } else {
            return json(['code' => 4002, 'msg' => '参数错误', 'data' => false]);
        }
    }

    /**
     * 优惠券叠加使用
     * 调用优惠券方法
     * @return \think\Response
     */
    public static function condition(Request $request)
    {
        //订单详情
        $order_no = $request->param('order_no');
        if (empty($order_no)) {
            return json(['code' => 4002, 'msg' => '参数无', 'data' => false]);
        }
        $oderInfo = Orders::detailOrder($order_no);

        $user_id = $oderInfo['user_id'];

        //总金额
        $total = $oderInfo['goods_total'];
        //获取用户的的优惠券
        $user_coupon = CouponModel::getUserCoupon($user_id, $total);

        $coupon = array_column($user_coupon, 'coupon');

        $data = self::recommendCoupon($coupon, $total);

        $sum = 0;
        foreach ($data as $v) {
            $sum = $sum + $v['coupon_price'];
        }
        if (empty($data)) {
            return json(['code' => 4001, 'msg' => '出错咯', 'data' => false]);
        } else {
            return json(['code' => 2001, 'msg' => '成功', 'data' => $data, 'discount_amount' => $sum]);
        }
    }

    /**
     * 优惠券列表
     */
    public static function couponList(Request $request)
    {
        //订单详情
        $order_no = $request->param('order_no');
        if (empty($order_no)) {
            return json(['code' => 4002, 'msg' => '参数无', 'data' => false]);
        }
        $oderInfo = Orders::detailOrder($order_no);
        $user_id = $oderInfo['user_id'];
        //总金额
        $total = $oderInfo['goods_total'];
        //获取用户的的优惠券
        $user_coupon = CouponModel::getUserCoupon($user_id, $total);
        $coupon = array_column($user_coupon, 'coupon');
        if (empty($coupon)) {
            return json(['code' => 4001, 'msg' => '出错咯', 'data' => false]);
        } else {
            return json(['code' => 2001, 'msg' => '成功', 'data' => $coupon]);
        }
    }

    /**
     * @return string|\think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 优惠券类型
     */
    public static function type()
    {
        $data = CouponType::select();
        $arr['code'] = 0;
        $arr['msg'] = '成功';
        $arr['count'] = count($data);
        $arr['data'] = $data;
        return json($arr);
    }

    /**
     * @param $couponArr
     * @param $status
     * @param $targetNum
     * @return array|mixed|\think\Model|null
     * 选择同类型中最优的优惠券
     */
    public static function selectCoupon($couponArr, $status, $targetNum)
    {
        if (empty($couponArr)) {
            return [];
        }
        //获取各种优惠券的最佳推荐使用的优惠券
        $array = array_column($couponArr, 'coupon_price');
        sort($array);
        $length = count($array);
        $left = 0;
        $right = 0;
        for ($right = $length - 1; $left != $right;) {
            $midIndex = ($right + $left) / 2;
            $mid = ($right - $left);
            $midValue = $array[$midIndex];
            if ($targetNum == $midValue) {
                return $midValue;
            }
            if ($targetNum > $midValue) {
                $left = $midIndex;
            } else {
                $right = $midIndex;
            }
            if ($mid <= 1) {
                break;
            }
        }
        $rightnum = $array[$right];
        $leftnum = $array[$left];

        //返还给优惠的优惠价格
        $ret = abs(($rightnum - $leftnum) / 2) > abs($rightnum - $targetNum) ? $leftnum : $rightnum;
        $couponInfo = CouponDsecModel::coupon($ret, $status);
        return $couponInfo;
    }

    /**
     * @param $coupon
     * @param $total
     * @return string|\think\response\Json
     * 优惠券的使用方案
     */
    public static function recommendCoupon($coupon, $total)
    {
        //定义各种类型的优惠券数组
        $coupon_arr1 = [];
        $coupon_arr2 = [];
        $coupon_arr3 = [];
        foreach ($coupon as $k => $v) {
            if ($v['use_min_price'] <= $total) {
                switch ($v['coupon_type']) {
                    case $v['coupon_type'] == 1;
                        array_push($coupon_arr1, $v);
                        break;
                    case $v['coupon_type'] == 2;
                        array_push($coupon_arr2, $v);
                        break;
                    case $v['coupon_type'] == 3;
                        array_push($coupon_arr3, $v);
                        break;
                }
            }
        }
        //获取各种优惠券的最佳推荐使用的优惠券
        $coupon1 = self::selectCoupon($coupon_arr1, 1, $total);
        $coupon2 = self::selectCoupon($coupon_arr2, 2, $total);
        $coupon3 = self::selectCoupon($coupon_arr3, 3, $total);
        $coupon = [];
        //推荐使用优惠券的方案
        if (empty($coupon1) && empty($coupon2) && empty($coupon3)) {
            return $coupon;
        } else if (empty($coupon1) && empty($coupon2)) {
            array_push($coupon, $coupon3);
        } else if (empty($coupon3) && empty($coupon1)) {
            array_push($coupon, $coupon2);
        } else if (empty($coupon3) && empty($coupon2)) {
            array_push($coupon, $coupon1);
        } else if (empty($coupon3)) {
            //使二种优惠券
            array_push($coupon, $coupon3);
            array_push($coupon, $coupon1);
        } else if (empty($coupon2)) {
            if ($total <= $coupon3['coupon_price']) {
                //无门槛券直接付完
                array_push($coupon, $coupon3);
            } else {
                //使用三种优惠券
                array_push($coupon, $coupon3);
                array_push($coupon, $coupon1);
            }
        } else if (empty($coupon1)) {
            if ($total <= $coupon3['coupon_price']) {
                //无门槛券直接付完
                array_push($coupon, $coupon3);
            } else {
                //使用三种优惠券
                array_push($coupon, $coupon3);
                array_push($coupon, $coupon1);
            }
        } else {
            if ($total <= $coupon3['coupon_price']) {
                //无门槛券直接付完
                array_push($coupon, $coupon3);
            } else {
                if ($total <= ($coupon3['coupon_price'] + $coupon1['coupon_price'])) {
                    //无门槛券+满减券
                    array_push($coupon, $coupon3);
                    array_push($coupon, $coupon1);
                } else {
                    //使用三种优惠券
                    array_push($coupon, $coupon3);
                    array_push($coupon, $coupon1);
                    array_push($coupon, $coupon2);
                }
            }
        }
        return $coupon;
    }
}
